<?php
// 应用公共文件



use think\facade\Cache;
use think\facade\Db;
use think\facade\Request;
use Firebase\JWT\JWT;
use Firebase\JWT\Key;
use think\facade\Config;
// 获取 当前平台/店铺 id（uniacid） (适用管理后台用户)
function current_uni_account(  ){

    $uniacid = Config::get('uniacid');

    if( !empty( $uniacid ) ){
       return $uniacid; 
    }

    $token = Request::param('token');
    $key = md5('字符串加密');
    $jwtAuth = JWT::decode($token,new Key($key,'HS256'));
    $authInfo = json_decode(json_encode($jwtAuth), true);
    $uid = $authInfo['user_id'];


    $uniacid = get_uniacid( $uid );

    if( !$uniacid ) Config::set('uniacid', $uniacid); // 完善后 ，需要注释掉
    // if( !$uniacid ) $uniacid=8; // 完善后 ，需要注释掉
    return $uniacid;

}

function random($length, $numeric = false) {
	$seed = base_convert(md5(microtime() . $_SERVER['DOCUMENT_ROOT']), 16, $numeric ? 10 : 35);
	$seed = $numeric ? (str_replace('0', '', $seed) . '012340567890') : ($seed . 'zZ' . strtoupper($seed));
	if ($numeric) {
		$hash = '';
	} else {
		$hash = chr(rand(1, 26) + rand(0, 1) * 32 + 64);
		--$length;
	}
	$max = strlen($seed) - 1;
	for ($i = 0; $i < $length; ++$i) {
		$hash .= $seed[mt_rand(0, $max)];
	}

	return $hash;
}

function get_uniacid( $uid )
    {

        $redis = Cache::store('redis')->handler();
        
        $uniacid = $redis->get('user::' . $uid . '::uniacid');
        
        return $uniacid;
    }

    function set_uniacid($uid, $uniacid )
    {

        $redis = Cache::store('redis')->handler();
        
        $uniacid = $redis->set('user::' . $uid . '::uniacid', $uniacid );
        
        return $uniacid;

    }

// 兼容微擎 pdo 函数
function pdo_query($sql, $params = array()) {
	// return pdo()->query($sql, $params);
    return Db::execute( $sql );
}


function pdo_fetchcolumn($sql, $params = array(), $column = 0) {
	// return pdo()->fetchcolumn($sql, $params, $column);
    $res = Db::query( $sql );
    return !empty($res) ? array_values($res[0])[$column] : null; 
}

function pdo_fetch($sql, $params = array()) {
	// return pdo()->fetch($sql, $params);
    $res = Db::query( $sql );
    return !empty($res) ? $res[0] : null; 
}

function pdo_fetchall($sql, $params = array(), $keyfield = '') {
	// return pdo()->fetchall($sql, $params, $keyfield);
    return Db::query( $sql );
}


function pdo_get($tablename, $condition = array(), $fields = array()) {
	// return pdo()->get($tablename, $condition, $fields);
}

function pdo_getall($tablename, $condition = array(), $fields = array(), $keyfield = '', $orderby = array(), $limit = array()) {
	// return pdo()->getall($tablename, $condition, $fields, $keyfield, $orderby, $limit);
}
function pdo_getslice($tablename, $condition = array(), $limit = array(), &$total = null, $fields = array(), $keyfield = '', $orderby = array()) {
	// return pdo()->getslice($tablename, $condition, $limit, $total, $fields, $keyfield, $orderby);
}

// 暂时注释掉 pdo_getcolumn：（不然打开调试就提示）在PHP 8.1及更高版本中，当可选参数出现在必需参数之前时，会被隐式视为必需参数。
// function pdo_getcolumn($tablename, $condition = array(), $field) {
	// return pdo()->getcolumn($tablename, $condition, $field);
// }


function pdo_exists($tablename, $condition = array()) {
	// return pdo()->exists($tablename, $condition);
}


function pdo_count($tablename, $condition = array(), $cachetime = 15) {
	// return pdo()->count($tablename, $condition, $cachetime);
}


function pdo_update($table, $data = array(), $params = array(), $glue = 'AND') {
	// return pdo()->update($table, $data, $params, $glue);
}


function pdo_insert($table, $data = array(), $replace = false) {
	// return pdo()->insert($table, $data, $replace);
}


function pdo_delete($table, $params = array(), $glue = 'AND') {
	// return pdo()->delete($table, $params, $glue);
}


function pdo_insertid( $tablename ) {
	// return pdo()->insertid();
    return Db::table( $tablename )->getLastInsID();
}


function pdo_begin() {
	// pdo()->begin();
}


function pdo_commit() {
	// pdo()->commit();
}


function pdo_rollback() {
	// pdo()->rollBack();
}

function pdo_debug($output = true, $append = array()) {
	// return pdo()->debug($output, $append);
}
function pdo_run($sql) {
	// return pdo()->run($sql);
}

function sm_pdo_getall( $tablename, $condition = array(), $fields = array(), $keyfield = '', $orderby = array(), $limit = array() ) {


    $sql = 'select * from ' . 'sh_' . $tablename ;

    // $where = implode( 'and', $condition );

    if( $condition ){
        $sql.= ' where ' . $condition;
    }
    

    if( $orderby ){
        $sql.= ' order by ' . $orderby;
    }

    return Db::query( $sql );
    
}

function sm_pdo_update( $tablename, $data = [], $where = []  ){

    $sql = 'update ' . 'sh_' . $tablename . ' set ' ;

    // $where = implode( 'and', $condition );

    $dataStr = '';
    foreach( $data as $key=> $val ){

        $dataStr .= $key . '=' . $val . ',';
    }
    
    $dataStr = trim($dataStr, ',');


    $whereStr = '';
    foreach( $where as $key=> $val ){

        $whereStr .= $key . '=' . $val . ' and ';
    }
    $whereStr = trim($whereStr, ' and ');

    $sql .= $dataStr . $whereStr;

    return Db::execute( $sql );

}